{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Bayesian methods for performing inference using neural networks\n",
    "\n",
    "Tom Charnock\n",
    "\n",
    "Institut d'Astrophysique de Paris\n",
    "<br><br><br>\n",
    "\n",
    "<div class=\"row\">\n",
    "    <div style=\"float: left; width: 20%; padding-right: 70px; padding-top:35px\">\n",
    "        <img src=\"../figures/SU.png\" alt=\"Sorbonne Université\" style=\"width:80%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 0px;\">\n",
    "        <img src=\"../figures/ANR.png\" alt=\"ANR\" style=\"width:40%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 5px;\">\n",
    "        <img src=\"../figures/IAP.png\" alt=\"IAP\" style=\"width:40%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 5px;\">\n",
    "        <img src=\"../figures/CNRS.png\" alt=\"CNRS\" style=\"width:30%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 5px;\">\n",
    "        <img src=\"../figures/Aquila.png\" alt=\"Aquila\" style=\"width:60%;\"/>\n",
    "    </div>\n",
    "</div> "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Neural networks for parameter regression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## What is a neural network?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<img style=\"max-width:60%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/neural_network_t.svg\">\n",
    "<br>\n",
    "<center>An arbitrary, non-linear function $(\\mathscr{f}:\\mathbb{R}^{\\bf d}\\rightarrow\\mathbb{R}^\\boldsymbol{t})$ with fittable parameters $(\\boldsymbol{w})$</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Let's talk about parameter estimation by regression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<img style=\"max-width:60%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/neural_network_IMNN.svg\">\n",
    "<br>\n",
    "<center>Take in data and <i>predict</i> model parameters</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Extremely popular for cosmological parameter estimation\n",
    "<br>\n",
    "<div style=\"float:left;width:40%\">\n",
    "    <img style=\"max-height:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/PI_list.png\">\n",
    "</div>\n",
    "<div style=\"float:left;width:60%\">\n",
    "    <img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/PI_plot.png\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Only provide intrinsically biased estimates!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Some stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### A model\n",
    "The generator $({\\bf d}\\in\\mathcal{M}(\\boldsymbol{\\theta}))$ of data $({\\bf d})$ with model parameters $(\\boldsymbol{\\theta})$ "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/physical_model.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### The likelihood and the posterior"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div style=\"float:left;width:60%\">\n",
    "    <br>\n",
    "    <img style=\"max-width:90%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/likelihood_posterior.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:40%\">\n",
    "    <b>Likelihood</b> $\\mathcal{L}({\\bf d}|\\boldsymbol{\\theta}^*)$<br>What is the probability that the model generates data ${\\bf d}$ given a set of model parameters $\\boldsymbol{\\theta}^*$?<br><br>\n",
    "    <b>Posterior</b> $\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d}^*)$<br>What is the probability that model parameters $\\boldsymbol{\\theta}$ generate the data ${\\bf d}^*$?\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Fisher information\n",
    "<br>\n",
    "<div style=\"float:left;width:60%\">\n",
    "    How much information does the data ${\\bf d}$ contain about the model parameters $\\boldsymbol{\\theta}$?\n",
    "    <br>\n",
    "    <br>\n",
    "    $${\\bf F}_{\\alpha\\beta} = \\left.\\left\\langle\\frac{\\partial^2\\ln\\mathcal{L}({\\bf d}|\\boldsymbol{\\theta})}{\\partial\\theta_\\alpha\\partial\\theta_\\beta}\\right\\rangle\\right|_{\\boldsymbol{\\theta}=\\boldsymbol{\\theta}^*}$$\n",
    "    <br>\n",
    "    For a Gaussian\n",
    "    <br>\n",
    "    $${\\bf F}_{\\alpha\\beta} = \\frac{\\partial\\mu}{\\partial\\theta_\\alpha}^T{\\bf C}^{-1}\\frac{\\partial\\mu}{\\partial\\theta_\\beta}.$$\n",
    "</div>\n",
    "<div style=\"float:left;width:40%\">\n",
    "    <br>\n",
    "    <img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/fisher.svg\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Maximum likelihood estimates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div style=\"float:left;width:40%\">\n",
    "    <br>\n",
    "        <img style=\"max-width:90%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/mle.gif\">\n",
    "</div>\n",
    "<div style=\"float:left;width:60%\">\n",
    "    Which value of the model parameters $\\boldsymbol{\\theta}$ is most likely to generate the observed population of data $\\{{\\bf d}\\}$?\n",
    "    <br>\n",
    "    <img style=\"max-width:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/MLE.svg\">\n",
    "    $$\\mathcal{L}(\\{{\\bf d}\\}|\\boldsymbol{\\theta}_\\textsf{MLE})=\\underset{\\boldsymbol{\\theta}\\in\\boldsymbol{\\Theta}}{\\textsf{Sup}}~\\mathcal{L}(\\{{\\bf d}\\}|\\boldsymbol{\\theta})$$\n",
    "</div>   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "###  Intrinsically biased estimators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div style=\"float:left;width:40%\">\n",
    "    <br>\n",
    "    <b>Intrinsically biased estimators</b><br><br>A summary of the parameters which is conditioned on the wrong likelihood.<br><br>\n",
    "</div>\n",
    "<div style=\"float:left;width:60%\">\n",
    "    <img style=\"max-width:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/biased_estimator.svg\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Training a network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div style=\"float:left;width:50%\">\n",
    "    <br>\n",
    "    <img style=\"max-width:90%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/Flat_likelihood_optimise.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:50%\">\n",
    "    Neural network is a model $\\mathcal{M}(\\boldsymbol{w}, {\\bf d})$ with parameters $\\boldsymbol{w}$ and initial conditions ${\\bf d}$.\n",
    "    <br><br>\n",
    "    Training amounts to finding the maximum likelihood estimates of the weights for a chosen <i>cost function</i> $\\boldsymbol{l}$.<br><br>\n",
    "</div>\n",
    "<br><br><br>\n",
    "<div style=\"float:left;width:100%\">\n",
    "    $$\\mathcal{L}(\\boldsymbol{l}\\,|\\boldsymbol{w}_\\textsf{MLE}, {\\bf d})=\\underset{\\boldsymbol{w}\\in\\boldsymbol{W}}{\\textsf{Sup}}~\\mathcal{L}(\\boldsymbol{l}\\,|\\boldsymbol{w}, {\\bf d})$$\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Why do neural networks give intrinsically biased estimates?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### The likelihood surface is not knowably convex (or concave)\n",
    "<br>\n",
    "<div style=\"float:left;width:50%\">\n",
    "    <img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/neural_network_t.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:50%\">\n",
    "    <img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/Flat_likelihood_theta.svg\">\n",
    "</div>\n",
    "<br>\n",
    "<div style=\"float:left;width:100%\">\n",
    "    <br>\n",
    "    <center>The likelihood surface is conditional on the architecture and the data</center>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### There is no known *correct* minimum which is complete for all data\n",
    "    \n",
    "<img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/non-interpolatable.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Even *well converged* networks are <u>unknowably</u> biased"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## In general outputs are highly informative\n",
    "<br>\n",
    "<div style=\"float:left;width:60%\">\n",
    "    <br>\n",
    "    <img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/correlation.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:40%\">\n",
    "    Outputs can <i>look</i> like model parameter estimates\n",
    "    <br>\n",
    "    <br>\n",
    "    They cannot be trusted as true predictions of the parameters\n",
    "    <br>\n",
    "    <br>\n",
    "    The can be used as informative summaries!\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## How can we use a neural network safely?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Build it into the physical model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/physical_model_nn.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Likelihood-free inference using machine learning"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Approximate Bayesian computation\n",
    "<br>\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/ABC.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Why even introduce a neural network?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## The curse of dimensionality\n",
    "<br>\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/ABC_2D.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Inadequate sampling\n",
    "\n",
    "Impossibly large numbers of simulations become necessary to correctly sample the posterior\n",
    "\n",
    "<img style=\"max-width:60%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/bad_posterior.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# We need to do some compression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Any pretrained neural network *could* be used"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### Is there a well motivated choice?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"max-height:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/IMNN.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Information maximising neural networks\n",
    "<br>\n",
    "<div style=\"float:left;width:60%\">\n",
    "    Which function $\\mathscr{f}: \\mathbb{R}^{\\bf d}\\to \\mathbb{R}^\\boldsymbol{\\theta}$ maximises the Fisher information of the summaries ${\\bf x}$ from that function?\n",
    "    <br>\n",
    "    <br>\n",
    "    <br>\n",
    "    $$\\mathcal{L}({\\bf d}|\\boldsymbol{\\theta})\\to\\mathcal{L}({\\bf x}|\\boldsymbol{\\theta}, {\\bf d})$$\n",
    "</div>\n",
    "<div style=\"float:left;width:40%\">\n",
    "    <img style=\"max-width:8\n",
    "                90%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"figures/F.gif\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Fisher information of the network summaries\n",
    "\n",
    "#### Simulations $\\{{\\bf d}_i|i\\in[1, n_{\\bf d}]\\}$ at a single parameter value $\\boldsymbol{\\theta}^*$\n",
    "\n",
    "#### Seed matched simulations $\\{{\\bf d}_i^{\\pm}|i\\in[1, n_{\\bf p}]\\}$ at perturbed fiducial paremeters $\\Delta\\boldsymbol{\\theta}^\\pm=\\boldsymbol{\\theta}\\pm\\boldsymbol{\\delta}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Calculate the covariance\n",
    "<img style=\"max-width:80%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/covariance.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Calculate the derivative of the mean of the summaries with respect to the parameters\n",
    "<img style=\"max-width:80%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/dmdt.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Calculate the Fisher information\n",
    "\n",
    "$${\\bf F}_{\\alpha\\beta}=\\frac{\\partial\\mu_\\mathscr{f}}{\\partial\\theta_\\alpha}^T{\\bf C}^{-1}_\\mathscr{f}\\frac{\\partial\\mu_\\mathscr{f}}{\\partial\\theta_\\beta}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### This Gaussian form forces the summaries to be Gaussianised\n",
    "<br>\n",
    "<center> Makes a non-linear mapping of (non-Gaussian) data to the compressed set of Gaussian summaries</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Optimise the tunable parameters of the network such that $\\textsf{ln}|{\\bf F}_{\\alpha\\beta}|$ is maximised!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Once converged the network compresses *and* Gaussianises the data *without* losing information*\n",
    "<br><br><br><br>\n",
    "*in the optimal case..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### And we get free maximum likelihood estimates...\n",
    "<br><br>\n",
    "$$\\boldsymbol{\\theta}^\\textsf{MLE}_\\alpha=\\boldsymbol{\\theta}_\\alpha^*+{\\bf F}_{\\alpha\\beta}^{-1}{\\bf C}_\\mathscr{f}^{-1}\\frac{\\partial\\mu_\\mathscr{f}}{\\partial\\theta_\\beta}({\\bf x}-\\mu_\\mathscr{f})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### And we can get free approximations of the posterior\n",
    "<br><br>\n",
    "$$\\textsf{Cov}[\\boldsymbol{\\theta}_\\alpha^\\textsf{MLE},\\boldsymbol{\\theta}_\\beta^\\textsf{MLE}] \\ge {\\bf F}_{\\alpha\\beta}^{-1}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Or do likelihood-free inference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### We pass our observed data through the network <br>(put it to the side)\n",
    "<img style=\"max-width:100%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/xobs.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Draw simulations (cleverly?) from the prior model parameters, $\\mathcal{P}(\\boldsymbol{\\theta})$ and pass them through the network\n",
    "<img style=\"max-width:100%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/xsims.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Measure the difference between the summarised simulations and the observed summary\n",
    "<br>\n",
    "<img style=\"max-width:60%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/rho.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<b>Compare distance between observed summaries and simulation summaries and select results within $\\epsilon$</b><br><br>\n",
    "<img style=\"max-width:70%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/ABC_IMNN.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Conclusions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### Neural networks are not to be trusted\n",
    "\n",
    "### They can make trusty companions - when the correct framework is introduced\n",
    "\n",
    "### Using statistics we can build them into the forward model to give us lossless summaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Great, but is there something better?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## We could infer the neural network..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## without *any* training data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"figures/NBM.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/NBM_square.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Our observation\n",
    "<img style=\"width:800px;max-height:100%;display:block;margin:auto;float:none!important;\" src=\"../figures/hmd.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## What does our observation actually look like\n",
    "<img style=\"width:600px;max-height:100%;display:block;margin:auto;float:none!important;\" src=\"figures/halo_catalogue.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### What does our observation actually actually look like\n",
    "<img style=\"width:400px;max-height:100%;display:block;margin:auto;float:none!important;\" src=\"figures/catalogue.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# What do we need to infer the parameters of a neural network?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Neural physical engines\n",
    "\n",
    "Build networks using physical principles.\n",
    "\n",
    "- Reduces number of parameters\n",
    "\n",
    "- Increases computational efficiency\n",
    "\n",
    "- Decreases overfitting\n",
    "\n",
    "- Improves interpretability"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Convolutions for translational invariance\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/convolutional_network.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "### Architecture suited to the data\n",
    "\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/spherical_convolutional_network.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Use only causally relevant features\n",
    "<img style=\"max-width:90%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/receptive_field.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Use only informative features\n",
    "<br>\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/kernels.svg\">\n",
    "<br>\n",
    "<sub><a href=\"https://github.com/tomcharnock/multipole_kernels\">github:multipole_kernels</a></sub>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Neural density estimators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Halo mass distribution function is a smooth function of mass given a density environment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### Use a mixture density network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"max-width:90%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/MDN.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Our neural density estimator\n",
    "$$\\begin{align*}\n",
    "        {\\tiny n(M|\\delta) =}&{\\tiny \\sum_i^N\\alpha(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\alpha}})\\mathcal{N}\\left(\\mu(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\mu}}), \\sigma(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\sigma}})|M\\right),}\\\\\n",
    "        {\\tiny =}&{\\tiny \\sum_i^N\\frac{\\alpha(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\alpha}})}{\\sqrt{2\\pi\\left(\\sigma(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\sigma}})\\right)^2}}\\exp\\left[-\\frac{\\left(\\log(M) - \\mu(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\mu}})\\right)^2}{2\\left(\\sigma(\\boldsymbol{\\psi}, \\boldsymbol{\\theta}_i^{\\boldsymbol{\\sigma}})\\right)^2}\\right],}\\end{align*}\n",
    "$$\n",
    "<div style=\"float:left;width:40%\">\n",
    "    <img style=\"max-width:100%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/MOG.svg\">\n",
    "</div>\n",
    "<div style=\"float:right;width:59%\">\n",
    "    $$\n",
    "    \\begin{align*}\n",
    "{\\tiny\\alpha_i} & {\\tiny=\\text{softplus}(w_{i}^\\alpha\\boldsymbol{\\psi}+b^\\alpha_i),}\\\\\n",
    "{\\tiny\\mu_i }&{\\tiny = \\left\\{\\begin{array}{ll}\n",
    "            w_{i}^\\mu\\boldsymbol{\\psi}+b^\\mu_i&i=0\\\\\n",
    "            \\textrm{Max}\\left[0,~ w_{i}^\\mu\\boldsymbol{\\psi}+b^\\mu_i\\right]+\\mu_{i-1}&i>0\\\\\n",
    "            \\end{array}\\right.,}\\\\\n",
    "{\\tiny\\sigma_i} &{\\tiny = \\text{softplus}(w_{i}^\\sigma\\boldsymbol{\\psi}+b^\\sigma_i)\\;,}\n",
    "    \\end{align*}\n",
    "    $$\n",
    "</div>\n",
    "<div style=\"float:left;width:100%\">\n",
    "<sub>2 Gaussians with<br> ${\\Tiny\\boldsymbol{b}^\\alpha \\to \\boldsymbol{b}^\\alpha +\\log(10^{-3})}$, ${\\Tiny b_0^\\mu \\to b_0^\\mu + \\log\\left(2\\times10^{12}\\right)}$ and ${\\Tiny\\boldsymbol{b}^\\sigma\\to \\boldsymbol{b}^\\alpha + \\log(10^3)}$.</sub>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Likelihood"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Poisson likelihood of the observed catalogue given the forward model\n",
    "<br><br>\n",
    "$$\\begin{align*}\n",
    "        {\\Tiny\\mathcal{L}  =}&{\\Tiny \\sum_{j\\in\\textsf{catalogue}}\\log\\left[\\sum_i^N\\frac{\\alpha_{i,j}}{\\sqrt{2\\pi\\sigma_{i,j}^2}}\\textsf{exp}\\left[-\\frac{\\left(\\textsf{ln}(M_j) - \\mu_{i,j}\\right)^2}{2\\sigma_{i,j}^2}\\right]\\right]}\\\\\n",
    "        &{\\Tiny - V\\sum_{j\\in\\textsf{voxels},i=1}^N\\frac{\\alpha_{i,j}}{2}\\textsf{exp}\\left[\\frac{\\sigma_{i,j}^2}{2}\\right]\\textsf{erfc}\\left[\\frac{\\textsf{ln}\\left(M_\\textsf{th}\\right) - \\mu_{i,j} - \\sigma_{i,j}^2}{\\sqrt{2\\sigma_{i,j}^2}}\\right].}\n",
    "    \\end{align*}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# HMCLET"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### We want to infer the weights of the neural bias model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Hamiltonian Monte Carlo\n",
    "<div style=\"float:left;width:60%\">\n",
    "<sub>Introduce momentum ${\\small {\\bf p}\\leftarrow\\mathcal{N}({\\bf 0},{\\bf M})}$ and solve Hamilton's equations</sub>\n",
    "<br><br>\n",
    "$$\\begin{align*}\n",
    "{\\Tiny\\mathcal{H}(\\boldsymbol{\\theta}, {\\bf p}) }&{\\Tiny~= \\mathcal{V}(\\boldsymbol{\\theta}) + \\mathcal{K}({\\bf p})}\\\\\n",
    "&{\\Tiny~=\\mathcal{L}(\\boldsymbol{\\theta}|\\boldsymbol{\\delta})-\\textsf{ln}\\left[\\pi(\\boldsymbol{\\theta})\\right]+\\frac{1}{2}{\\bf p}^T{\\bf M}^{-1}{\\bf p}}\n",
    "\\end{align*}$$\n",
    "<sub>Accept samples according to probability</sub>\n",
    "\n",
    "$$\\begin{equation*}\n",
    "    {\\Tiny\\alpha = \\textsf{Min}\\left[\\textsf{exp}\\left(\\Delta\\mathcal{H}\\right), 1\\right]}\n",
    "\\end{equation*}$$\n",
    "</div>\n",
    "<div style=\"float:right;width:35%\">\n",
    "    <br><br>\n",
    "    <img style=\"max-width:100%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/HMC.svg\">\n",
    "    <br>\n",
    "</div>\n",
    "<div style=\"float:left;width:100%\">\n",
    "Evolve using ${\\Tiny\\dot{\\boldsymbol{\\theta}}= {\\bf M}^{-1}{\\bf p}}$ and ${\\Tiny\\dot{\\bf p}= -\\nabla\\mathcal{V}(\\boldsymbol{\\theta})}$\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "### Acceptance is guaranteed if ODEs are perfectly solved"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "# Use leapfrog algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Leapfrog algorithm\n",
    "\n",
    "<img style=\"max-width:60%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/leapfrog.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Acceptance criterion\n",
    "\n",
    "<img style=\"max-width:60%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/leapfrog_accept.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## How does this work for neural networks?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Likelihood surface is extremely flat and highly degenerate\n",
    "<br>\n",
    "<img style=\"max-width:70%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/correlated_likelihood.svg\">\n",
    "\n",
    "Nearly impossible to know the mass matrix, ${\\bf M}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Use second order geometric information\n",
    "<img style=\"max-width:90%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/second_order.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "### Quasi-Newtonian inverse Hessian\n",
    "\n",
    "$$\\begin{equation*}\n",
    "        {\\Tiny{\\bf B}^*=\\left(\\mathbb{I}-\\frac{\\boldsymbol{s}_i\\boldsymbol{y}_i^T}{\\boldsymbol{y}_i^T\\boldsymbol{s}_i}\\right){\\bf B}_i\\left(\\mathbb{I}-\\frac{\\boldsymbol{y}_i\\boldsymbol{s}_i^T}{\\boldsymbol{y}_i^T\\boldsymbol{s}_i}\\right)+\\frac{\\boldsymbol{s}_i\\boldsymbol{s}_i^T}{\\boldsymbol{s}_i^T\\boldsymbol{y}_i},}\n",
    "    \\end{equation*}\n",
    "$$\n",
    "<sub>with ${\\Tiny\\boldsymbol{s}_i=\\boldsymbol{\\theta}^*-\\boldsymbol{\\theta}_i}$ and ${\\Tiny\\boldsymbol{y}_i=\\nabla\\mathcal{L}(\\boldsymbol{\\theta}^*|\\boldsymbol{\\delta})-\\nabla\\mathcal{L}(\\boldsymbol{\\theta}_i|\\boldsymbol{\\delta})}$.</sub>\n",
    "\n",
    "#### Both $\\boldsymbol{s}$ and $\\boldsymbol{y}$ come for free with the leapfrog algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "## We can now successfully traverse the likelihood surface\n",
    "\n",
    "The momenta and trajectories get rescaled by the Hessian providing a surface which is not as highly degenerate or flat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## How well does it work?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/NBM_square.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Sample the weights of the neural bias model\n",
    "<br>\n",
    "<img style=\"width:900px;max-width:100%;max-height:100%;display:block;margin:auto;float:none!important;\" src=\"../figures/weight_trace.svg\">\n",
    "\n",
    "✓ Weight values are properly sampled after burn in<br>\n",
    "✓ NPE acts as a contrast enhancer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### See the effects of the neural physical engine\n",
    "<br>\n",
    "<img style=\"width:500px;max-height:100%;display:block;margin:auto;float:none!important;\" src=\"../figures/3D_projections.svg\">\n",
    "\n",
    "✓ Non-local information is used to improve fit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# And what does the halo mass distribution function look like?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Halo mass distribution function from neural bias model\n",
    "<br>\n",
    "<img style=\"width:800px;max-height:100%;display:block;margin:auto;float:none!important;\" src=\"../figures/hmdf.svg\">\n",
    "✓ Fits data!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Second conclusion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### We can infer neural networks built for physics\n",
    "\n",
    "### Highly efficient and bias free"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Take home message"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Stop doing machine learning, think, then start doing machine learning again!"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Diaporama",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
